package org.example.mybatis_demo.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatis_demo.model.UserInfo;
import org.example.mybatis_demo.service.UserService;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserInfoMapper {

    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where create_time = #{createTime}")
    List<UserInfo> selectByCreateFlag(LocalDateTime time);

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoAll1(Integer deleteFlag);

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoAll21(Integer deleteFlag);

//    MyBatis会自动将Java参数的值传递给SQL查询中的占位符。
    @Select("select * from userinfo where delete_flag = #{ff} and age = #{age}")
    List<UserInfo> getUserInfoAll2(@Param("ff") Integer flag, Integer age);  // 加注解 参数重命名

//    增 删 改 返回参数为 Integer  返回的是 改变后影响的行数
    @Insert("insert into userinfo (username,password,age,gender,phone) values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(String username,String password,Integer age,Integer gender,String phone);

    @Insert("INSERT INTO userinfo (username, password, age, gender, phone) VALUES (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    UserInfo insert1(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id") // 获取自增主键
    @Insert("INSERT INTO userinfo (username, password, age, gender, phone) VALUES (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert2(UserInfo userInfo);

//    如果使用参数重命名 形式为对象.属性
    @Insert("INSERT INTO userinfo (username, password, age, gender, phone) VALUES (#{user.username}, #{user.password}, #{user.age}, #{user.gender}, #{user.phone})")
    UserInfo insert3(@Param("user") UserInfo userInfo);

//    删除操作
    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

//    更新操作
    @Update("update userinfo set age = #{age}, password = #{password} where id = #{id}")
    Integer update(UserInfo userInfo);


//    由于数据库规范是采用蛇形(例: create_time) 而 Java代码采用驼峰(createTime) 导致关系映射失败 因此可以采用起别名的方式 AS
    @Select("select id,username,password,age,gender,phone,delete_flag as deleteFlag ,create_time as createFlag,update_time as updateTime from userinfo")
    List<UserInfo> getUserInfoAll3();

//    可以采用 注解 解决不匹配的问题
    @Results(id = "111",value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createFlag"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll4();

//    结果复用
    @ResultMap("111")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll5();

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoAll41(Integer deleteFlag);

    @Select("select * from userinfo where username = #{username}")
    List<UserInfo> getUserName(String name);

}
